<template>
  <div class="loginBox">
    <div class="login">
      <h1>密码登录</h1>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            type="text"
            autocomplete="off"
            placeholder="Username"
            :prefix-icon="User"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
            show-password
            placeholder="Password"
            :prefix-icon="Lock"
          />
        </el-form-item>
        <el-form-item prop="captcha">
          <el-row :gutter="12">
            <el-col :span="12">
              <el-input
                v-model.number="ruleForm.captcha"
                :prefix-icon="Key"
                placeholder="图形验证"
              />
            </el-col>
            <el-col :span="12">
              <img :src="captchaImg" alt="验证图片" @click="getCaptchaImg" />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

// 引入icon图标
import { User, Lock, Key } from '@element-plus/icons-vue'

// 引入接口 --- 获取验证图片 / 登录
import { getLoginCaptchaData, login } from '@/api/loginApi'

// 路由
import { useRouter } from 'vue-router'
const router = useRouter()

// 引入 login 仓库
import loginStore from '@/stores/modules/loginStore'
import userStore from '@/stores/modules/userStore'
const loginInfo = loginStore()
const userInfo = userStore()

// 登录信息
const ruleForm = ref({
  username: '',
  password: '',
  captcha: ''
})

// 登录验证信息
const rules = ref({
  username: [{ required: true, message: '请输入用户名!', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码!', trigger: 'blur' }],
  captcha: [{ required: true, message: '请输入验证码!', trigger: 'blur' }]
})

// 获取表单组件实例
const ruleFormRef = ref()

// 验证图片
const captchaImg = ref('')

// 获取验证图片
function getCaptchaImg() {
  getLoginCaptchaData().then((res) => {
    captchaImg.value = res.data.img
    loginInfo.token = res.data.key
  })
}
getCaptchaImg()

// 点击登录的方法
const submitForm = (formEl: any) => {
  if (!formEl) return
  formEl.validate((valid: any) => {
    if (valid) {
      login({
        username: ruleForm.value.username,
        password: ruleForm.value.password,
        captcha: ruleForm.value.captcha,
        key: JSON.parse(localStorage.getItem('loginInfo') as string).token
      }).then((res) => {
        if (res.data.errNo === 0) {
          loginInfo.token = res.data.context.jwt
          userInfo.acl = res.data.context.acl
          userInfo.username = ruleForm.value.username
          router.push('/dashboard/welcome')
        } else {
          getCaptchaImg()
        }
      })
    } else {
      console.log('error submit!')
      return false
    }
  })
}
</script>

<style scoped lang="scss">
.loginBox {
  height: 100%;
  background-image: url('	http://39.97.100.253:90/static/media/bg.ad243b2593cb3063d2ac.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 500px;
  min-width: 350px;
  .login {
    padding: 24px;
    background-color: #fff;
    width: 252px;
    height: 317px;
    h1 {
      text-align: center;
      font-weight: 500;
    }
    ::v-deep(.el-form-item__content) {
      width: 100%;
      margin: 0 !important;
      .el-col {
        display: flex;
        img {
          cursor: pointer;
        }
      }
      .el-input__wrapper {
        padding: 6.5px 11px;
      }
      .el-button {
        width: 100%;
        height: 40px;
        font-size: 16px;
      }
    }
  }
}
</style>
